import Vue from 'vue'
var UtilsPlugin={};

var Utils = {
    /**
     * 分转元
     * @param fen 可以是一个对象,或者一个数字
     * @param props  对象中需要转换的属性列表
     * @returns {number}
     */
    fenToYuan:function(fen,props){
        try{
            if(null == fen){
                return 0;
            }
            if(!props){
                return parseInt(fen)/100;
            }else{
                for(var index in props){
                    if(fen[props[index]]){
                        fen[props[index]]=parseInt(fen[props[index]])/100;
                    }
                }
                return fen;
            }
        }catch (e){
            return fen;
        }
    },
    /**
     * 元转分
     * @param yuan
     * @param props   如果传入该参数,说明yuan参数是个对象,props是需要转换的属性列表
     * @param notCopy  默认会copy一个新的对象出来
     * @returns {*}
     */
    yuanToFen:function(yuan,props,notCopy){
        if(!props){
            return parseInt(yuan * 100);
        }else{
            var newObj=yuan;
            if(!notCopy){
                newObj=JSON.parse( JSON.stringify( yuan ) );
            }
            for(var index in props){
                if(newObj[props[index]]){
                    newObj[props[index]]=newObj[props[index]]*100;
                }
            }
            return newObj;
        }
    },
    numToChinese:function(num){
        if(num){
            switch(num){
                case 1: return "一";
                case 2: return "二";
                case 3: return "三";
                case 4: return "四";
                case 5: return "五";
                case 6: return "六";
                case 7: return "七";
            }
        }else{
            return "";
        }
    },
    removeHtmlTag:function(str){
        if(str){
            return str.replace(/<[^>]+>/g,"");
        }else{
            return "";
        }
    },
    createCookie:function(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        value=encodeURIComponent(value);
        document.cookie = name + "=" + value + expires + "; path=/";
    },
    readCookie:function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) {
                return decodeURIComponent(c.substring(nameEQ.length, c.length));
            }
        }
        return null;
    },
    eraseCookie:function(name) {
        Utils.createCookie(name, "", -1);
    }
};

UtilsPlugin.install = function (Vue, options) {
    Vue.prototype.$util = Utils;
};

export default UtilsPlugin;